package com.maxio.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maxio.common.enums.ErrorCode;
import com.maxio.domain.dto.EmpDTO.EmpAddDTO;
import com.maxio.domain.dto.EmpDTO.EmpEditDTO;
import com.maxio.domain.dto.PageDTO;
import com.maxio.domain.po.Emp;
import com.maxio.domain.query.EmpQuery;
import com.maxio.domain.vo.EmpVO;
import com.maxio.mapper.EmpMapper;
import com.maxio.service.IEmpService;
import com.maxio.utils.ThrowUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author enhuan
 * @since 2024-05-28
 */
@Service
@RequiredArgsConstructor
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements IEmpService {

    private final EmpMapper empMapper;


    /**
     * 新增员工信息
     *
     * @param addDTO@return 结果
     */
    @Override
    public Long saveEmp(EmpAddDTO addDTO) {
        Emp emp = BeanUtil.copyProperties(addDTO, Emp.class);
        emp.setId((long) RandomUtil.randomInt(100,999999));
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        int result = empMapper.insert(emp);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, "新增员工信息失败");
        return emp.getId();
    }

    /**
     * 根据id查询员工信息
     * @param id 员工id
     * @return 结果
     */
    @Override
    public Emp selectById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR, ErrorCode.PARAMS_ERROR.getMessage());

        Emp emp = empMapper.selectById(id);
        ThrowUtils.throwIf(emp == null, ErrorCode.NOT_FOUND_ERROR, ErrorCode.NOT_FOUND_ERROR.getMessage());

        return emp;
    }

    /**
     * 修改员工信息
     * @param editDTO 员工信息
     * @return 结果
     */
    @Override
    public Boolean updateEmpById(EmpEditDTO editDTO) {
        Emp emp = BeanUtil.copyProperties(editDTO, Emp.class);
        emp.setUpdateTime(LocalDateTime.now());
        int result = empMapper.updateById(emp);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, ErrorCode.OPERATION_ERROR.getMessage());
        return Boolean.TRUE;
    }

    /**
     * 删除员工信息
     * @param id 员工id
     * @return 结果
     */
    @Override
    public Boolean deleteById(Long id) {
        ThrowUtils.throwIf(id == null, ErrorCode.PARAMS_ERROR, ErrorCode.PARAMS_ERROR.getMessage());
        int result = empMapper.deleteById(id);
        ThrowUtils.throwIf(result <= 0, ErrorCode.OPERATION_ERROR, ErrorCode.OPERATION_ERROR.getMessage());
        return Boolean.TRUE;
    }

    /**
     * 分页查询员工信息
     * @param empQuery 查询条件
     * @return 结果
     */
    @Override
    public PageDTO<EmpVO> selectPage(EmpQuery empQuery) {
        String username = empQuery.getUsername();
        String name = empQuery.getName();
        Integer gender = empQuery.getGender();
        Integer job = empQuery.getJob();
        Long deptId = empQuery.getDeptId();

        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNoneBlank(username), Emp::getUsername, username)
                .eq(StringUtils.isNoneBlank(name), Emp::getName, name)
                .eq(gender != null, Emp::getGender, gender)
                .eq(job != null, Emp::getJob, job)
                .eq(deptId != null, Emp::getDeptId, deptId);

        Page<Emp> p = new Page<>(empQuery.getCurrent(), empQuery.getPageSize());
        IPage<Emp> page = empMapper.selectPage(p, wrapper);
        return new PageDTO<>(page.getTotal(), page.getPages(), BeanUtil.copyToList(page.getRecords(), EmpVO.class));
    }

}
